package cn.lysj.framework.common.security;

import cn.hutool.core.util.StrUtil;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.service.PermissionService;
import cn.lysj.framework.common.service.UserService;
import cn.lysj.framework.common.utils.web.ParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Exrickx
 */
@Slf4j
@Component
public class UserDetailsServiceImpl implements UserDetailsService {



    @Autowired
    private UserService userService;
    @Resource
    private PermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findByUsername(username);
        if(user == null){
            user = new User();
        }
        if(!ParamUtil.isBlank(user.getId())){
            user.setPermissions(permissionService.findByUserId(user.getId()));
        }
        return new SecurityUserDetails(user);
    }
}
